#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from VectorTestSequence import VectorTestSequence
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from base.ChoicesModifier import ChoicesModifier


# This test verifies that vector widening and narrowing floating-point instructions can be
# generated and executed successfully.
class MainSequence(VectorTestSequence):
    def __init__(self, aGenThread, aName=None):
        super().__init__(aGenThread, aName)

        self._mInstrList = (
            "VFNCVT.F.F.W##CALM",
            "VFNCVT.F.X.W##CALM",
            "VFNCVT.F.XU.W##CALM",
            "VFNCVT.ROD.F.F.W##CALM",
            "VFNCVT.RTZ.X.F.W##CALM",
            "VFNCVT.RTZ.XU.F.W##CALM",
            "VFNCVT.X.F.W##CALM",
            "VFNCVT.XU.F.W##CALM",
            "VFNMACC.VF##CALM",
            "VFNMACC.VV##CALM",
            "VFNMADD.VF##CALM",
            "VFNMADD.VV##CALM",
            "VFNMSAC.VF##CALM",
            "VFNMSAC.VV##CALM",
            "VFNMSUB.VF##CALM",
            "VFNMSUB.VV##CALM",
            "VFWADD.VF##CALM",
            "VFWADD.VV##CALM",
            "VFWADD.WF##CALM",
            "VFWADD.WV##CALM",
            "VFWCVT.F.F.V##CALM",
            "VFWCVT.F.X.V##CALM",
            "VFWCVT.F.XU.V##CALM",
            "VFWCVT.RTZ.X.F.V##CALM",
            "VFWCVT.RTZ.XU.F.V##CALM",
            "VFWCVT.X.F.V##CALM",
            "VFWCVT.XU.F.V##CALM",
            "VFWMACC.VF##CALM",
            "VFWMACC.VV##CALM",
            "VFWMSAC.VF##CALM",
            "VFWMSAC.VV##CALM",
            "VFWMUL.VF##CALM",
            "VFWMUL.VV##CALM",
            "VFWNMACC.VF##CALM",
            "VFWNMACC.VV##CALM",
            "VFWNMSAC.VF##CALM",
            "VFWNMSAC.VV##CALM",
            "VFWREDOSUM.VS##CALM",
            "VFWREDUSUM.VS##CALM",
            "VFWSUB.VF##CALM",
            "VFWSUB.VV##CALM",
            "VFWSUB.WF##CALM",
            "VFWSUB.WV##CALM",
        )

    # Set up the environment prior to generating the test instructions.
    def _setUpTest(self):
        choices_mod = ChoicesModifier(self.genThread)

        # Ensure element width is at least 16, as 8-bit floating point operations are illegal
        vsew_choice_weights = {
            "0x0": 0,
            "0x1": 10,
            "0x2": 10,
            "0x3": 0,
            "0x4": 0,
            "0x5": 0,
            "0x6": 0,
            "0x7": 0,
        }
        choices_mod.modifyRegisterFieldValueChoices("vtype.VSEW", vsew_choice_weights)

        # Ensure vector register group size is no more than 4, as larger values are not legal for
        # widening and narrowing instructions
        vlmul_choice_weights = {
            "0x0": 10,
            "0x1": 10,
            "0x2": 10,
            "0x3": 0,
            "0x4": 0,
            "0x5": 10,
            "0x6": 10,
            "0x7": 10,
        }
        choices_mod.modifyRegisterFieldValueChoices("vtype.VLMUL", vlmul_choice_weights)

        choices_mod.commitSet()

    # Return the maximum number of test instructions to generate.
    def _getMaxInstructionCount(self):
        return 1000

    # Return a list of test instructions to randomly choose from.
    def _getInstructionList(self):
        return self._mInstrList


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
